<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="类型" prop="nftType">
              <el-select v-model="queryParams.nftType" placeholder="请选择" clearable >
                <el-option v-for="dict in nft_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="装备名称" prop="title">
              <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="价格" prop="price">
              <el-input v-model="queryParams.price" placeholder="请输入价格" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="鉴定" prop="jd">
              <el-select v-model="queryParams.jd" placeholder="请选择" clearable >
                <el-option  key="未鉴定" label="未鉴定" value="未鉴定"/>
                <el-option  key="已鉴定" label="已鉴定" value="已鉴定"/>
              </el-select>
            </el-form-item>
            <el-form-item label="装备等级" prop="zbdj">
              <el-input v-model="queryParams.zbdj" placeholder="请输入装备等级" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="财宝值 " prop="cbz">
              <el-input v-model="queryParams.cbz" placeholder="请输入财宝值 " clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="混沌能量 " prop="hdnl">
              <el-input v-model="queryParams.hdnl" placeholder="请输入混沌能量 " clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label-width="130" label="单位财富成本" prop="dwcbz">
              <el-input v-model="queryParams.dwcbz" placeholder="请输入单位财富成本" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="继承财富" prop="jccfz">
              <el-input v-model="queryParams.jccfz" placeholder="请输入继承财富" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label-width="130" label=" 继承单位财富成本" prop="jccfcb">
              <el-input v-model="queryParams.jccfcb" placeholder="请输入继承单位财富成本" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['nft:item:add']">新增</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['nft:item:edit']">修改</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['nft:item:remove']">删除</el-button>-->
<!--          </el-col>-->
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['nft:item:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="itemList"
                :default-sort="defaultSort"
                @sort-change="handleSortChange"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标题" align="center" prop="title" >
            <template #default="scope">
                <a :href="'https://app.actpass.com/nftDetail?id=' + scope.row.itemId +'&goods_no=' + scope.row.goodsNo" target="_blank" class="buttonText">{{scope.row.title}}</a>
            </template>
        </el-table-column>
        <el-table-column label="类型" align="center" prop="nftType">
          <template #default="scope">
            <dict-tag :options="nft_type" :value="scope.row.nftType"/>
          </template>
        </el-table-column>
        <el-table-column sortable label="价格" align="center" prop="price" />
        <el-table-column sortable label="是否鉴定  " align="center" prop="jd" />
        <el-table-column sortable label="装备等级" align="center" prop="zbdj" />
        <el-table-column sortable label="财宝值 " align="center" prop="cbz" />
        <el-table-column sortable label="混沌能量 " align="center" prop="hdnl" />
        <el-table-column sortable label="单位财富成本" align="center" prop="dwcbz" />
        <el-table-column sortable label="继承财富" align="center" prop="jccfz" />
        <el-table-column sortable label="继承单位财富成本" align="center" prop="jccfcb" />
        <el-table-column sortable label="综合性价比" align="center" prop="zhxjb" />
        <el-table-column sortable label="物品更新版本号" align="center" prop="versionNum" >
        </el-table-column>
        <el-table-column sortable label="数据更新时间" align="center" prop="getAttributeTime" />



<!--        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--          <template #default="scope">-->
<!--            <el-tooltip content="查看" placement="top">-->
<!--              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>-->
<!--            </el-tooltip>-->
<!--&lt;!&ndash;            <el-tooltip content="修改" placement="top">&ndash;&gt;-->
<!--&lt;!&ndash;              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nft:item:edit']"></el-button>&ndash;&gt;-->
<!--&lt;!&ndash;            </el-tooltip>&ndash;&gt;-->
<!--&lt;!&ndash;            <el-tooltip content="删除" placement="top">&ndash;&gt;-->
<!--&lt;!&ndash;              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nft:item:remove']"></el-button>&ndash;&gt;-->
<!--&lt;!&ndash;            </el-tooltip>&ndash;&gt;-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改nft对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="itemFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="1.珍宝 2.混沌源晶 3.混沌余烬" prop="nftType">
          <el-select v-model="form.nftType" placeholder="请选择1.珍宝 2.混沌源晶 3.混沌余烬">
            <el-option
                v-for="dict in nft_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="是否鉴定  " prop="jd">
          <el-input v-model="form.jd" placeholder="请输入是否鉴定  " />
        </el-form-item>
        <el-form-item label="装备等级" prop="zbdj">
          <el-input v-model="form.zbdj" placeholder="请输入装备等级" />
        </el-form-item>
        <el-form-item label="财宝值 " prop="cbz">
          <el-input v-model="form.cbz" placeholder="请输入财宝值 " />
        </el-form-item>
        <el-form-item label="混沌能量 " prop="hdnl">
          <el-input v-model="form.hdnl" placeholder="请输入混沌能量 " />
        </el-form-item>
        <el-form-item label="单位财富成本  单位成本=卖价0.1÷财富值10=0.01 " prop="dwcbz">
          <el-input v-model="form.dwcbz" placeholder="请输入单位财富成本  单位成本=卖价0.1÷财富值10=0.01 " />
        </el-form-item>
        <el-form-item label="继承财富 继承财富=财富值×0.8  " prop="jccfz">
          <el-input v-model="form.jccfz" placeholder="请输入继承财富 继承财富=财富值×0.8  " />
        </el-form-item>
        <el-form-item label=" 继承单位财富成本=售价÷继承财富 " prop="jccfcb">
          <el-input v-model="form.jccfcb" placeholder="请输入 继承单位财富成本=售价÷继承财富 " />
        </el-form-item>
        <el-form-item label="完整数据包" prop="data">
            <el-input v-model="form.data" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="详情属性" prop="attribute">
            <el-input v-model="form.attribute" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="物品id" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入物品id" />
        </el-form-item>
        <el-form-item label="版本时间戳" prop="versionNum">
          <el-input v-model="form.versionNum" placeholder="请输入版本时间戳" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Item" lang="ts">
import { listItem, getItem, delItem, addItem, updateItem } from '@/api/nft/item';
import { ItemVO, ItemQuery, ItemForm } from '@/api/nft/item/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { nft_type } = toRefs<any>(proxy?.useDict('nft_type'));

const itemList = ref<ItemVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const defaultSort = ref<any>({ prop: 'price', order: 'descending' });

const queryFormRef = ref<ElFormInstance>();
const itemFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ItemForm = {
  id: undefined,
  nftType: undefined,
  title: undefined,
  price: undefined,
  jd: undefined,
  zbdj: undefined,
  cbz: undefined,
  hdnl: undefined,
  dwcbz: undefined,
  jccfz: undefined,
  jccfcb: undefined,
  data: undefined,
  attribute: undefined,
  itemId: undefined,
  versionNum: undefined,
  remark: undefined,
}
const data = reactive<PageData<ItemForm, ItemQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    nftType: undefined,
    title: undefined,
    price: undefined,
    jd: undefined,
    zbdj: undefined,
    cbz: undefined,
    hdnl: undefined,
    dwcbz: undefined,
    jccfz: undefined,
    jccfcb: undefined,
    data: undefined,
    attribute: undefined,
    itemId: undefined,
    versionNum: undefined,
    orderByColumn: defaultSort.value.prop,
    isAsc: defaultSort.value.order,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    nftType: [
      { required: true, message: "1.珍宝 2.混沌源晶 3.混沌余烬不能为空", trigger: "change" }
    ],
    title: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "价格不能为空", trigger: "blur" }
    ],
    jd: [
      { required: true, message: "是否鉴定  不能为空", trigger: "blur" }
    ],
    zbdj: [
      { required: true, message: "装备等级不能为空", trigger: "blur" }
    ],
    cbz: [
      { required: true, message: "财宝值 不能为空", trigger: "blur" }
    ],
    hdnl: [
      { required: true, message: "混沌能量 不能为空", trigger: "blur" }
    ],
    dwcbz: [
      { required: true, message: "单位财富成本  单位成本=卖价0.1÷财富值10=0.01 不能为空", trigger: "blur" }
    ],
    jccfz: [
      { required: true, message: "继承财富 继承财富=财富值×0.8  不能为空", trigger: "blur" }
    ],
    jccfcb: [
      { required: true, message: " 继承单位财富成本=售价÷继承财富 不能为空", trigger: "blur" }
    ],
    data: [
      { required: true, message: "完整数据包不能为空", trigger: "blur" }
    ],
    attribute: [
      { required: true, message: "详情属性不能为空", trigger: "blur" }
    ],
    itemId: [
      { required: true, message: "物品id不能为空", trigger: "blur" }
    ],
    versionNum: [
      { required: true, message: "版本时间戳不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询nft列表 */
const getList = async () => {
  loading.value = true;
  const res = await listItem(queryParams.value);
  itemList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  itemFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: ItemVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加nft";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: ItemVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getItem(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改nft";
}

/** 排序触发事件 */
const handleSortChange = (column: any) => {
  queryParams.value.orderByColumn = column.prop;
  queryParams.value.isAsc = column.order;
  getList();
};

/** 提交按钮 */
const submitForm = () => {
  itemFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateItem(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addItem(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: ItemVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除nft编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delItem(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('nft/item/export', {
    ...queryParams.value
  }, `item_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
